﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using QuanLyRapChieuPhim.DTO;
using System.ComponentModel;

namespace QuanLyRapChieuPhim.DAO
{
    class KhachHangDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<KhachHangDTO> GetList()
        {

            DataTable dt = new DataTable();
            BindingList<KhachHangDTO> ds = new BindingList<KhachHangDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_KhachHang", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.MaKhachHang = int.Parse(row["maKhachHang"].ToString());
                    KhachHang.TenKhachHang = row["tenKhachHang"].ToString();
                    KhachHang.CMND = row["CMND"].ToString();
                    KhachHang.NgaySinh = DateTime.Parse(row["ngaySinh"].ToString());
                    KhachHang.GioiTinh = (row["gioiTinh"].ToString() == "True")?"Nam":"Nữ";
                    KhachHang.DiaChi = row["diaChi"].ToString();
                    KhachHang.DienThoai = row["dienThoai"].ToString();
                    ds.Add(KhachHang);
                }
                return ds;
            }
            return null;
        }

        public BindingList<KhachHangDTO> GetList_QL()
        {

            DataTable dt = new DataTable();
            BindingList<KhachHangDTO> ds = new BindingList<KhachHangDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_KhachHang_QL", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.MaKhachHang = int.Parse(row["maKhachHang"].ToString());
                    KhachHang.TenKhachHang = row["tenKhachHang"].ToString();
                    KhachHang.CMND = row["CMND"].ToString();
                    KhachHang.NgaySinh = DateTime.Parse(row["ngaySinh"].ToString());
                    KhachHang.GioiTinh = (row["gioiTinh"].ToString() == "True")?"Nam":"Nữ";
                    KhachHang.DiaChi = row["diaChi"].ToString();
                    KhachHang.DienThoai = row["dienThoai"].ToString();
                    KhachHang.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(KhachHang);
                }
                return ds;
            }
            return null;
        }

        public BindingList<KhachHangDTO> GetList_QL_DaXoa()
        {

            DataTable dt = new DataTable();
            BindingList<KhachHangDTO> ds = new BindingList<KhachHangDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_KhachHang_QL_DaXoa", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.MaKhachHang = int.Parse(row["maKhachHang"].ToString());
                    KhachHang.TenKhachHang = row["tenKhachHang"].ToString();
                    KhachHang.CMND = row["CMND"].ToString();
                    KhachHang.NgaySinh = DateTime.Parse(row["ngaySinh"].ToString());
                    KhachHang.GioiTinh = (row["gioiTinh"].ToString() == "True")?"Nam":"Nữ";
                    KhachHang.DiaChi = row["diaChi"].ToString();
                    KhachHang.DienThoai = row["dienThoai"].ToString();
                    KhachHang.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(KhachHang);
                }
                return ds;
            }
            return null;
        }

        public BindingList<KhachHangDTO> GetListLikeName_QL(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<KhachHangDTO> ds = new BindingList<KhachHangDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_KhachHang_QL", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.MaKhachHang = int.Parse(row["maKhachHang"].ToString());
                    KhachHang.TenKhachHang = row["tenKhachHang"].ToString();
                    KhachHang.CMND = row["CMND"].ToString();
                    KhachHang.NgaySinh = DateTime.Parse(row["ngaySinh"].ToString());
                    KhachHang.GioiTinh = (row["gioiTinh"].ToString() == "True") ? "Nam" : "Nữ";
                    KhachHang.DiaChi = row["diaChi"].ToString();
                    KhachHang.DienThoai = row["dienThoai"].ToString();
                    KhachHang.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(KhachHang);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<KhachHangDTO> GetListLikeName_QL_DaXoa(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<KhachHangDTO> ds = new BindingList<KhachHangDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_KhachHang_QL_DaXoa", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.MaKhachHang = int.Parse(row["maKhachHang"].ToString());
                    KhachHang.TenKhachHang = row["tenKhachHang"].ToString();
                    KhachHang.CMND = row["CMND"].ToString();
                    KhachHang.NgaySinh = DateTime.Parse(row["ngaySinh"].ToString());
                    KhachHang.GioiTinh = (row["gioiTinh"].ToString() == "True") ? "Nam" : "Nữ";
                    KhachHang.DiaChi = row["diaChi"].ToString();
                    KhachHang.DienThoai = row["dienThoai"].ToString();
                    KhachHang.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(KhachHang);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<KhachHangDTO> GetListExport()
        {

            DataTable dt = new DataTable();
            BindingList<KhachHangDTO> ds = new BindingList<KhachHangDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetListExport_KhachHang", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.MaKhachHang = int.Parse(row["maKhachHang"].ToString());
                    KhachHang.TenKhachHang = row["tenKhachHang"].ToString();
                    KhachHang.CMND = row["CMND"].ToString();
                    KhachHang.NgaySinh = DateTime.Parse(row["ngaySinh"].ToString());
                    KhachHang.GioiTinh = (row["gioiTinh"].ToString() == "True") ? "Nam" : "Nữ";
                    KhachHang.DiaChi = row["diaChi"].ToString();
                    KhachHang.DienThoai = row["dienThoai"].ToString();
                    KhachHang.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(KhachHang);
                }
                return ds;
            }
            return null;
        }

        public KhachHangDTO GetFromId(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetFromID_KhachHang", sqlParameter);
                dp.CloseConnect();
                KhachHangDTO KhachHang = new KhachHangDTO();
                foreach (DataRow row in dt.Rows)
                {
                    KhachHang.MaKhachHang = int.Parse(row["maKhachHang"].ToString());
                    KhachHang.TenKhachHang = row["tenKhachHang"].ToString();
                    KhachHang.CMND = row["CMND"].ToString();
                    KhachHang.NgaySinh = DateTime.Parse(row["ngaySinh"].ToString());
                    KhachHang.GioiTinh = (row["gioiTinh"].ToString() == "True") ? "Nam" : "Nữ";
                    KhachHang.DiaChi = row["diaChi"].ToString();
                    KhachHang.DienThoai = row["dienThoai"].ToString();
                }
                return KhachHang;
            }
            return null;
        }

        public int GetIDFromCMND(int _CMND)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@CMND", _CMND) };
                dt = dp.SqlExcuteQuery("uproc_GetIDFromCMND_KhachHang", sqlParameter);
                dp.CloseConnect();
                KhachHangDTO KhachHang = new KhachHangDTO();
                foreach (DataRow row in dt.Rows)
                {
                    KhachHang.MaKhachHang = int.Parse(row["maKhachHang"].ToString());
                }
                if (dt.Rows.Count > 0)
                    return int.Parse(dt.Rows[0][0].ToString());
                return 0;
            }
            return -1;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_KhachHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_KhachHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(KhachHangDTO _KH)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@TenKhachHang", _KH.TenKhachHang),
                    new SqlParameter("@CMND", _KH.CMND), 
                    new SqlParameter("@NgaySinh", _KH.NgaySinh), 
                    new SqlParameter("@GioiTinh", (_KH.GioiTinh == "Nam")?1:0), 
                    new SqlParameter("@DiaChi", _KH.DiaChi), 
                    new SqlParameter("@DienThoai", _KH.DienThoai)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_KhachHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public bool Add_Import(KhachHangDTO _KH)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@TenKhachHang", _KH.TenKhachHang),
                    new SqlParameter("@CMND", _KH.CMND), 
                    new SqlParameter("@NgaySinh", _KH.NgaySinh), 
                    new SqlParameter("@GioiTinh", (_KH.GioiTinh == "Nam")?1:0), 
                    new SqlParameter("@DiaChi", _KH.DiaChi), 
                    new SqlParameter("@DienThoai", _KH.DienThoai),
                    new SqlParameter("@DaXoa", _KH.DaXoa)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_Import_KhachHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public bool IsExists(KhachHangDTO _KH)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@TenKhachHang", _KH.TenKhachHang),
                    new SqlParameter("@CMND", _KH.CMND), 
                    new SqlParameter("@NgaySinh", _KH.NgaySinh), 
                    new SqlParameter("@GioiTinh", (_KH.GioiTinh == "Nam")?1:0), 
                    new SqlParameter("@DiaChi", _KH.DiaChi), 
                    new SqlParameter("@DienThoai", _KH.DienThoai)
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_IsExists_KhachHang", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count > 0)
                    return true;
                return false;
            }
            return false;
        }

        public bool Update(KhachHangDTO _KH)
        {
            bool kq;
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaKhachHang", _KH.MaKhachHang),
                    new SqlParameter("@TenKhachHang", _KH.TenKhachHang),
                    new SqlParameter("@CMND", _KH.CMND), 
                    new SqlParameter("@NgaySinh", _KH.NgaySinh), 
                    new SqlParameter("@GioiTinh", (_KH.GioiTinh == "Nam")?1:0), 
                    new SqlParameter("@DiaChi", _KH.DiaChi), 
                    new SqlParameter("@DienThoai", _KH.DienThoai)
                };
                kq = dp.SqlExcuteNonQuery("uproc_Update_KhachHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
    }
}
